home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 14642 / 14642.xpi / chrome / modules / trackers / search.js < prev    next >
Text File  |  2009-10-19  |  4KB  |  103 lines

  1. /* Copyright 2009, Boomtango.com.  All Rights Reserved. */
  2. /* search.js
  3.  * Responsible for tracking search results
  4.  */
  5.  
  6. var EXPORTED_SYMBOLS = ["search"];
  7. var search = {
  8.     name: "Search",
  9.     name_plural: "Searches",
  10.     color: "#9F3628",
  11.     isInternal: true,
  12.     preview: function(doc, node, data){
  13.         if(data.preview){
  14.             var result = this.json.decode(data.preview);
  15.             var len = result.results.length;
  16.             if(len){
  17.                 var searchTitle = doc.createElement("label");
  18.                 searchTitle.className = "searchPreviewTitle";
  19.                 searchTitle.setAttribute("flex", "1");
  20.                 searchTitle.setAttribute("crop", "end");
  21.                 searchTitle.setAttribute("value",
  22.                     this.app.getString("type.search.title")
  23.                 );
  24.                 node.appendChild(searchTitle);
  25.                 
  26.                 for(var x = 0; x < len; x++){
  27.                     var item = result.results[x];
  28.                     
  29.                     var hbox = doc.createElement("hbox");
  30.                     node.appendChild(hbox);
  31.                     
  32.                     var imgBox = doc.createElement("box");
  33.                     imgBox.setAttribute("align", "center");
  34.                     imgBox.setAttribute("pack", "center");
  35.                     imgBox.className = "searchPreviewBulletBox";
  36.                     
  37.                     hbox.appendChild(imgBox);
  38.                     
  39.                     var img = doc.createElement("image");
  40.                     img.setAttribute("src", "chrome://boomtango/skin/bullet_circle.png");
  41.                     imgBox.appendChild(img);
  42.                     
  43.                     var label = doc.createElement("label");
  44.                     label.className = "text-link searchPreviewResult";
  45.                     label.setAttribute("value", item.title);
  46.                     label.setAttribute("flex", "1");
  47.                     label.setAttribute("href", item.url);
  48.                     label.setAttribute("crop", "end");
  49.                     hbox.appendChild(label);
  50.                 }
  51.             }
  52.         }
  53.     },
  54.     onLoadTracks: function(url, doc){
  55.         var result = [];
  56.         if(this.pattern.exec(url)){
  57.             var qr = url.match(/q=([^&]+)(&|$)/);
  58.             var query = "";
  59.             if(qr.length >= 2){
  60.                 query = qr[1];
  61.             }
  62.             result.push(
  63.                 {
  64.                     type: "search",
  65.                     preview: this._buildSnippet(doc, query),
  66.                     content: query
  67.                 }
  68.             );
  69.         }
  70.         return result;
  71.     },
  72.  
  73.     _buildSnippet: function(doc, query){
  74.         var result = { query: query, results: []};
  75.         if(doc){
  76.             var a = doc.getElementsByTagName("a");
  77.             var len = a.length;
  78.             var maxfound = 10;
  79.             for(var i =0; i < len; i++){
  80.                 var item = a[i];
  81.                 if(item.className == "l" &&
  82.                 item.parentNode && item.parentNode.className == "r" &&
  83.                 item.parentNode.nextSibling && 
  84.                 (item.parentNode.nextSibling.className== "s" ||
  85.                     item.parentNode.nextSibling.tagName == 'SPAN')){
  86.                     var entry = { title: item.textContent, url: item.href };
  87.                     if(!maxfound){
  88.                         break;
  89.                     } else {
  90.                         result.results.push(entry);
  91.                         maxfound--;
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.  
  97.         return this.json.encode(result);
  98.     },
  99.     json: Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON),
  100.     pattern: /(http:\/\/www\.google\..+\/.*[?&]q=([^&]+))|(http:\/\/[-a-zA-Z]+\.start3\.mozilla\.com\/search\?.*[?&]q=([^&]+))/
  101.  
  102. };
  103.